﻿#include "mainwindow.h"

#include <QApplication>

//OSG
#include <osgQOpenGL/osgQOpenGLWidget>
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osg/CoordinateSystemNode>
#include <osg/Switch>
#include <osg/Types>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/SphericalManipulator>
#include <osgGA/Device>
//OSG end

//osgEarth
#include <osgEarth/EarthManipulator>
#include <osgEarth/Map>
#include <osgEarth/MapNode>
#include <osgEarth/GDAL>
#include <osgEarth/TMS>

//osgEarth end

#include <vector>
#include <string>
using namespace std;


int main(int argc, char *argv[])
{
    QString version = "v1.0.0.1" ;//2024/3/17

    QApplication a(argc, argv);

    osg::ArgumentParser arguments(&argc, argv);

    MainWindow w;
    w.setWindowTitle( QString("SimpleEarth") + " " + version ) ;
    int windowWidth =w.size().width();
    int windowHeight = w.size().height();
    osgQOpenGLWidget widget(&w);
    QObject::connect(&widget, &osgQOpenGLWidget::initialized,[&widget,&arguments,&windowWidth,&windowHeight]
        {
            widget.getOsgViewer()->addEventHandler(new osgViewer::WindowSizeHandler);
            //widget.getOsgViewer()->setUpViewInWindow(0,0, windowWidth,windowHeight );
            //widget.getOsgViewer()->setUpViewerAsEmbeddedInWindow(0,0,windowWidth,windowHeight  );

            widget.getOsgViewer()->setCameraManipulator( new osgEarth::EarthManipulator(arguments) );
            widget.getOsgViewer()->getCamera()->setSmallFeatureCullingPixelSize(-1.0f);

            //by https://www.cnblogs.com/charlee44/p/17770785.html
            QSize size = widget.size();
            float aspectRatio =
                static_cast<float>(size.width()) / static_cast<float>(size.height());
            widget.getOsgViewer()->getCamera()->setProjectionMatrixAsPerspective(
                60.f, aspectRatio, 1.f, 1000.f);

            string tifFile = "E:/coding/osgearth2023/osgearth3.4/build/bin/data/world.tif";
            osgEarth::Map* map = new osgEarth::Map() ;
            osgEarth::GDALImageLayer::Options gdalOpt ;
            gdalOpt.url() = tifFile ;
            osgEarth::GDALImageLayer* imgLayer = new osgEarth::GDALImageLayer(gdalOpt);
            map->addLayer(imgLayer ) ;

            osgEarth::TMSImageLayer* tmslayer = new osgEarth::TMSImageLayer();
            tmslayer->setURL( osgEarth::URI("http://readymap.org/readymap/tiles/1.0.0/7/"));
            map->addLayer(tmslayer);

            osgEarth::TMSElevationLayer* elevation = new osgEarth::TMSElevationLayer;
            elevation->setURL( osgEarth::URI("http://readymap.org/readymap/tiles/1.0.0/116/")) ;
            elevation->setVerticalDatum("egm96");
            map->addLayer(elevation);

            osgEarth::MapNode* mapNode = new osgEarth::MapNode(map);
            if(!mapNode)
            {
                std::cout << "bad map node." << std::endl;
                return 1;
            }
            widget.getOsgViewer()->setSceneData(mapNode);
            return 0;
        });



    w.setCentralWidget(&widget);
    w.show();

    return a.exec();
}
